package com.hhkj.api.train.service.biz.impl;

import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.bank.common.cont.BankConst;
import com.hhkj.admin.bank.domain.Bank;
import com.hhkj.admin.bank.domain.BankRule;
import com.hhkj.admin.bank.service.extend.BankRuleExtendService;
import com.hhkj.admin.course.domain.Course;
import com.hhkj.admin.course.service.base.CourseService;
import com.hhkj.admin.train.domain.Train;
import com.hhkj.admin.train.service.base.TrainService;
import com.hhkj.api.bank.service.biz.ApiBankBizService;
import com.hhkj.api.common.session.holder.ApiStudentSessionHolder;
import com.hhkj.api.common.session.holder.model.LoginStudent;
import com.hhkj.api.train.common.component.corrector.ApiTrainCorrector;
import com.hhkj.api.train.common.component.creator.ApiTrainCreator;
import com.hhkj.api.train.model.info.req.TrainStageReq;
import com.hhkj.api.train.model.info.req.TrainSubmitReq;
import com.hhkj.api.train.model.info.res.*;
import com.hhkj.api.train.service.biz.ApiTrainBizService;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ApiTrainServiceImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/5/24 16:04
 */
@Service
public class ApiTrainBizServiceImpl implements ApiTrainBizService {
    @Autowired
    private BankRuleExtendService ruleExtendService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TrainService trainService;
    @Autowired
    private ApiTrainCreator apiTrainCreator;
    @Autowired
    private ApiTrainCorrector apiTrainCorrectFactory;
    @Autowired
    private ApiBankBizService apiBankBizService;

    @Override
    public ApiTrainTestRes startTrainByBankId(Long bankId) {
        return apiTrainCreator.createTrainByBankId(bankId);
    }

    @Override
    public TrainBankRes getPractiseBank() {
        Bank publishBank = apiBankBizService.getPublishBank(BankConst.Type.PRACTISE.getCode());
        return new TrainBankRes(publishBank.getQuestionAmount());
    }

    @Override
    public TrainCorrectRes getCorrectResById(Long id) {
        Long currentStudentId = ApiStudentSessionHolder.getStudentId();
        return apiTrainCorrectFactory.getStudentCorrectRes(id, currentStudentId);
    }

    @Override
    public TrainCorrectRes submit(TrainSubmitReq req) {
        return apiTrainCorrectFactory.correctTrain(req);
    }

    @Override
    public ApiTrainPractiseRes startPractiseTrain(String modeCode) {
        return apiTrainCreator.createPractiseTrain(modeCode);
    }

    @Override
    public ApiTrainContinuePractiseRes continuePractiseTrain() {
        return apiTrainCreator.createContinuePractiseTrain(false);
    }

    @Override
    public ApiTrainTestRes startTestTrain() {
        return apiTrainCreator.createTestTrain();
    }

    @Override
    public Train getLastTrain(String typeCode) {
        LoginStudent loginStudent = ApiStudentSessionHolder.getLoginStudent();
        Train trainDo = new Train();
        trainDo.setStudentId(loginStudent.getId());
        trainDo.setCourseId(loginStudent.getCourseId());
        trainDo.setTypeCode(typeCode);
        trainDo.setCourseId(loginStudent.getCourseId());
        return trainService.getLastTrainByDo(trainDo);
    }

    @Override
    public Course getCourse() {
        LoginStudent loginStudent = ApiStudentSessionHolder.getLoginStudent();
        Course coursePo = courseService.getLevelNumThirdPoById(loginStudent.getCourseId());
        if (ObjectUtil.isNull(coursePo)) {
            throw new GlobalException("科目信息不存在");
        }
        if (SysConst.Flag.IS.getFlag().equals(coursePo.getDisableFlag())) {
            throw new GlobalException("科目信息不存在");
        }
        return coursePo;
    }

    @Override
    public List<String> listPractiseModeCode() {
        LoginStudent loginStudent = ApiStudentSessionHolder.getLoginStudent();
        BankRule rulePo = ruleExtendService.getPoByCourseIdAndCourseBankTypeIdNotNull(
                loginStudent.getCourseId(), loginStudent.getCourseBankTypeId());
        return rulePo.getPracticeModeCodeList();
    }

    @Override
    public void stage(TrainStageReq req) {
        apiTrainCreator.stageTrain(req);
    }

    @Override
    public ApiTrainContinuePractiseRes stageContinuePractiseTrain() {
        return apiTrainCreator.createContinuePractiseTrain(true);
    }
}
